package cn.john.org.service.impl;

import cn.john.basic.service.impl.BaseServiceImpl;
import cn.john.basic.util.PageList;
import cn.john.org.domain.Department;
import cn.john.org.mapper.DepartmentMapper;
import cn.john.org.query.DepartmentQuery;
import cn.john.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 1. 实现基础接口中的方法
     *    如果实现类中的方法和base实现中的方法不一致，需要一一重写
     * @param department
     */
    @Override
    @Transactional()
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if(department.getParent() == null || department.getParent().getId() == null){
            dirPath ="/" + department.getId();
        }else{
            //2.不是顶级
            //2.1 需要根据父亲id查询出父亲的dirpath
            Department parent = departmentMapper.loadById(department.getParent().getId());
            //2.2 在拼接上我自己的id就可以了
            dirPath = parent.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /* 2. 实现扩展接口 */
    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.loadOneAndTwoLevelTree();
    }
}
